<template>
  <div class="big-box">
    <div class="img-box">
      <div
        class="show-box"
        :style="{
          transform: 'translateX(' + translate + 'px)',
          transition: tsion ? 'all 0.5s' : 'none',
        }"
      >
        <img src="@/assets/image/banner6.jpg" alt="" />
        <img v-for="item in src" :src="item" alt="" :key="item.index" />
        <img src="@/assets/image/banner1.jpg" alt="" />
      </div>
    </div>
    <div class="arrowhead-box">
      <span @click="last">&lt;</span>
      <span @click="next">&lt;</span>
    </div>
    <div class="swiper-box" ref="swiper">
      <span
        v-for="(item, index) in src"
        @click="swiper(index)"
        :key="item.index"
      ></span>
    </div>
  </div>
</template>
<script>
export default {
  name: "",
  components: {},
  props: [""],
  watch: {
    translateX: {
      handler(val) {
        let a = this.$refs.swiper.querySelectorAll("span");
        a.forEach((element) => {
          element.style.width = "12px";
        });

        if (this.translateX < 0) {
          val = this.src.length - 1;
        }
        if (this.translateX > this.src.length - 1) {
          val = 0;
        }
        a[val].style.width = "100px";
      },
    },
  },
  computed: {
    translate() {
      return -(this.translateX + 1) * 1200;
    },
  },
  data() {
    return {
      src: [
        require("@/assets/image/banner1.jpg"),
        require("@/assets/image/banner2.jpg"),
        require("@/assets/image/banner4.jpg"),
        require("@/assets/image/banner5.jpg"),
        require("@/assets/image/banner6.jpg"),
      ],
      translateX: 0,
      tsion: true,
    };
  },
  created() {},
  mounted() {
    setInterval(() => {
      this.next();
    }, 3000);
  },
  methods: {
    // 上一张
    last() {
      this.translateX--;
      this.tsion = true;
      if (this.translateX < 0) {
        setTimeout(() => {
          this.tsion = false;
          this.translateX = this.src.length - 1;
        }, 500);
      }
    },
    // 下一张
    next() {
      this.translateX++;
      this.tsion = true;
      if (this.translateX > this.src.length - 1) {
        setTimeout(() => {
          this.tsion = false;
          this.translateX = 0;
        }, 500);
      }
    },
    swiper(i) {
      this.translateX = i;
    },
  },
};
</script>
<style lang='less' scoped>
.big-box {
  position: relative;
  width: 100%;
  height: 350px;
  background-color: #999;
  margin: 100px auto;
  //   box-shadow: 0 0 3pc rgba(0, 0, 0, 0.4);
  border-radius: 10px;
}

.img-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.show-box {
  display: flex;
  height: 100%;
  width: 100%;
  transition: all 0.5s;
}

.show-box img {
  float: left;
  min-width: 1200px;
  min-height: 350px;
}

.arrowhead-box {
  position: absolute;
  top: 40%;
  float: left;
  width: 100%;
  height: 50px;
}

.arrowhead-box span {
  float: left;
  display: block;
  width: 60px;
  height: 60px;
  border-radius: 50px;

  background-position: 6px 14px;
  cursor: pointer;
  opacity: 0.5;
  color: #fff;
  font-size: 50px;
  font-weight: 600;
  margin: 20px;
}

.arrowhead-box span:nth-child(2) {
  float: right;
  transform: rotate(180deg);
}

.arrowhead-box span:hover {
  opacity: 1;
}

.swiper-box {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.swiper-box span {
  float: left;
  width: 12px;
  height: 12px;
  background-color: white;
  border-radius: 50px;
  margin-left: 10px;
  cursor: pointer;
  transition: all 0.5s ease-out;
}

.swiper-box span:nth-child(1) {
  width: 100px;
}
@media only screen and (max-width: 992px) {
  .arrowhead-box {
    display: none;
  }
  .big-box {
    height: 200px;
  }
}
</style>